#ifndef MODULE_IO_H
#define MODULE_IO_H

#include "defines.h"

#include <avr/io.h>
#include <avr/interrupt.h>

#include "time.h"
#include "module_events.h"
#include "usart.h"
#include "lcd.h"
#include "mcp2515.h"

//Potentiometer filtering settings
//
//Number of samples for average value
#define POTFILTERSIZE 10
//Disregard values that differ from the average for more than:
#define POTDISREGARDTHERESHOLD 255
#define POTLASTSAMPLEWEIGHT 5

typedef enum {btn0, btn1, btn2, btn3, btn4, \
			joy_up, joy_down, joy_left, joy_right, joy_enter}Button ;
#define BUTTON_SIZE 10

typedef enum {pota, potb}Potentiometer;
#define POTENTIOMETER_SIZE 2

typedef struct
{
	uint8_t min;
	uint8_t max;
	uint8_t mid;
	// <128 less sensitive at the center, exp at the endpoints
	// 128 linear
	// >128 more sensitive at the center, less at the endpoints
	uint8_t exp;
}Calibration, pota_calib, potb_calib;




extern void module_io_init(void);
extern void module_io_run(void);
extern int8_t buttonpressed(Button button);
extern int8_t buttonheld(Button button, uint16_t length);
extern uint8_t potvalue(Potentiometer pot);
extern uint8_t _potaverage(Potentiometer pot);
extern uint8_t _potlastsample(Potentiometer pot);
extern void _adc_select(Potentiometer pot);
extern Potentiometer _adc_selected(void);
/*
extern void savepotcalib_min(enum Potentiometer pot);
extern void savepotcalib_max(enum Potentiometer pot);
extern void savepotcalib_mid(enum Potentiometer pot);
extern void savepotcalib_exp(enum Potentiometer pot, uint8_t value);
*/

#define _dispwm() DDRD &= ~(1<<PD7)
#define _enpwm() DDRD |= (1<<PD7)
extern void sound(uint16_t hz, uint16_t time);
extern void print_io_info(void);
#ifdef MODULE_IO_UNITTEST
extern uint8_t module_io_unittest(void);
#endif
#endif

